package com.gyms.service;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.classic.okhttp.Cons;
import com.classic.okhttp.beans.HVClickInfoBean;
import com.gyms.activity.MainActivity;
import com.gyms.constant.Constants;
import com.gyms.manager.BannerIntentManger;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import constant.EventConstant;
import utils.StringUtils;
import weight.UiEvent;

/**
 * ltx on 2016/12/22 10:32
 *  继承 GTIntentService 接收来⾃自个推的消息, 所有消息在线程中回调, 如果注册了了该服务, 则务必要在 A
    ndroidManifest中声明, 否则⽆无法接受消息<br>
 * onReceiveMessageData 处理理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理理回执 <br>
 */
public class IntentService extends GTIntentService {
    private BannerIntentManger bannerIntentManger;//banner 的点击事件
    public IntentService() {

    }
    @Override
    public void onReceiveServicePid(Context context, int pid) {

    }
    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        if(null==bannerIntentManger){
            bannerIntentManger=new BannerIntentManger(context);
        }
        byte[] payload = msg.getPayload();
        String message = new String(payload);
        Log.i(TAG, "onReceiveMessageData: " + message);
        try {
            JSONObject jsonObject = new JSONObject(message);
            doGetClickInfo(jsonObject);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     *包装成点击对象
     * @param jsonObject json
     */

    private void doGetClickInfo(JSONObject jsonObject) {
        try {
            boolean  mRequireLogin=jsonObject.getBoolean("requireLogin");
            int  action=jsonObject.getInt("action");
            String  title=jsonObject.getString("title");
            String  url=jsonObject.getString("url");
            HVClickInfoBean mClickInfo=new HVClickInfoBean();
            mClickInfo.setAction(action);
            mClickInfo.setTitle(title);
            mClickInfo.setUrl(url);
            mClickInfo.setRequireLogin(mRequireLogin);
            doPushJump(mClickInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * push 跳转
     * @param mClickInfo 点击对象
     */

    private void doPushJump(HVClickInfoBean mClickInfo) {
        if(StringUtils.isNull(mClickInfo.getUrl())) {
           return;
        }
        String[] gymIds = mClickInfo.getUrl().split(";");
        if(gymIds.length>0) {
                goMainActivity(mClickInfo);
        }
    }

    /**
     * 跳转详情
     * @param mClickInfo 跳转详情
     */

    private void goMainActivity(HVClickInfoBean mClickInfo) {
        Intent mIntent= new Intent(IntentService.this, MainActivity.class);
        Bundle mBundle=new Bundle();
        mBundle.putSerializable(Constants.S_KEY_CLICKINFOBEAN,mClickInfo);
        mIntent.putExtras(mBundle);
        startActivity(mIntent);

    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
        Cons.mClientid=clientid;
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_DO_REGIST_PUSH,clientid));
        EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_DO_LOAD_CITY_PUSH,clientid));
    }
    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }
    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {

    }
}
